NPM CheatSheet

November 07, 2024

Resources

Installation

Install via Node.js:

npm install --global npm

Check installation:

npm --version

Usage

Initialize a new project:

npm init

Install a package:

npm install <package-name>

Install a package as a development dependency:

npm install --save-dev <package-name>

Install a package globally:

npm install --global <package-name>

Update a package:

npm update <package-name>

Remove a package:

npm uninstall <package-name>

Commands

  • npm init: Initialize a new project.
  • npm install <package>: Install a package.
  • npm install --save-dev <package>: Install a package as a development dependency.
  • npm install --global <package>: Install a package globally.
  • npm update <package>: Update a package.
  • npm uninstall <package>: Remove a package.

Example

npm install lodash
npm install --save-dev jest
npm install --global nodemon
npm update lodash
npm uninstall lodash

Configuration

Create a package.json file:

npm init --yes

Set the registry:

npm config set registry https://registry.npmjs.org/

Check the registry:

npm config get registry

Scripts

Add a script to package.json:

"scripts": {
  "start": "node index.js"
}

Run a script:

npm run start

Versioning

Check the version of a package:

npm show <package-name> version

Check outdated packages:

npm outdated

Update all packages:

npm update

Publishing

Login to NPM:

npm login

Publish a package:

npm publish

Update the package version:

npm version <update_type>

Troubleshooting

Clear the cache:

npm cache clean --force

Check for vulnerabilities:

npm audit

Fix vulnerabilities:

npm audit fix

Profile picture

Written by Manthan Ankolekar who lives and works in Karnataka, India. You should follow them on Twitter